package com.wzd.testimage.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;

/**
 * Created by hello on 15-6-1.
 */
public class MyTextView extends TextView {
    private String text;
    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float len = getTextSize()*(text.length()/3);
        Shader shader = new LinearGradient(0,0,len,0,new int[]{Color.RED,Color.BLACK,Color.GREEN},new float[]{0.4f,0.5f,0.6f}, Shader.TileMode.MIRROR);
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setTextSize(getTextSize());
        canvas.drawText(text,0,50,paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        postInvalidate();
        return super.onTouchEvent(event);
    }
  public void  setText(String str){
        text=str;
    }
}
